# -*- coding:utf-8 -*-
"""Created By Corvo"""

import mod.server.extraServerApi as serverApi
from mod.common.minecraftEnum import ItemPosType

from skill_entity import SkillEntity


class LivingEntity(SkillEntity):
    """生物实体基类"""
    __mVersion__ = 9

    def SetCanFly(self, active):
        # type: (bool) -> None
        """设置可以飞行状态"""
        self.comp_factory.CreateFly(self.id).ChangePlayerFlyState(active)

    def SetTameOwner(self, ownerId):
        # type: (str) -> bool
        """设置实体的属主"""
        return self.comp_factory.CreateActorOwner(self.id).SetEntityOwner(ownerId)

    def SetTame(self, ownerId):
        # type: (str) -> bool
        """设置实体驯服"""
        return self.comp_factory.CreateTame(self.id).SetEntityTamed(ownerId, self.id)

    def GetTameOwner(self):
        """获得实体驯服主人"""
        return self.comp_factory.CreateTame(self.id).GetOwnerId()

    # -----------------------------------------------------------------------------------

    """装备相关"""

    # 增加装备耐久
    def AddArmorDuration(self, slot, delta):
        # type: (int, int) -> bool
        """增加装备耐久"""
        item_comp = self.comp_factory.CreateItem(self.id)
        data = item_comp.GetItemDurability(ItemPosType.ARMOR, slot)
        return item_comp.SetItemDurability(ItemPosType.ARMOR, slot, data + delta)

    # 设置装备耐久最大值
    def SetArmorDurationMax(self, slot, value):
        # type: (int, int) -> bool
        """设置装备耐久最大值"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetItemDurability(ItemPosType.ARMOR, slot, value)

    # 增加手持物品耐久
    def AddSelectedItemDuration(self, delta):
        # type: (int) -> bool
        """增加手持物品耐久"""
        item_comp = self.comp_factory.CreateItem(self.id)
        data = item_comp.GetItemDurability(ItemPosType.CARRIED, 0)
        return item_comp.SetItemDurability(ItemPosType.CARRIED, 0, data + delta)

    # 设置手持物品耐久最大值
    def SetSelectedItemDurationMax(self, value):
        # type: (int) -> bool
        """设置手持物品耐久最大值"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetItemMaxDurability(ItemPosType.CARRIED, 0, value)

    # 增加副手物品耐久
    def AddOffhandItemDuration(self, delta):
        # type: (int) -> bool
        """增加副手物品耐久"""
        item_comp = self.comp_factory.CreateItem(self.id)
        data = item_comp.GetItemDurability(ItemPosType.OFFHAND, 0)
        return item_comp.SetItemDurability(ItemPosType.OFFHAND, 0, data + delta)

    # 设置副手物品耐久最大值
    def SetOffhandItemDurationMax(self, value):
        # type: (int) -> bool
        """设置副手物品耐久最大值"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetItemMaxDurability(ItemPosType.OFFHAND, 0, value)

    # 增加槽位物品耐久
    def AddSlotItemDuration(self, slot, delta):
        # type: (int, int) -> bool
        """增加槽位物品耐久"""
        item_comp = self.comp_factory.CreateItem(self.id)
        data = item_comp.GetItemDurability(ItemPosType.INVENTORY, slot)
        return item_comp.SetItemDurability(ItemPosType.INVENTORY, slot, data + delta)

    # 设置槽位物品耐久最大值
    def SetSlotItemDurationMax(self, slot, value):
        # type: (int, int) -> bool
        """设置槽位物品耐久最大值"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetItemMaxDurability(ItemPosType.INVENTORY, slot, value)

    # -----------------------------------------------------------------------------------

    """骑乘相关"""

    # 设置生物骑乘位置
    def SetRidePos(self, pos):
        # type: (tuple) -> bool
        """设置生物骑乘位置"""
        return self.comp_factory.CreateRide(serverApi.GetLevelId()).SetRidePos(self.id, pos)

    # 获得骑乘者信息
    def GetRiders(self):
        # type: () -> list
        """获得骑乘者信息"""
        return self.comp_factory.CreateRide(self.id).GetRiders()

    # 获得正在骑乘生物Id
    def GetOnRideEntity(self):
        # type: () -> str
        """获得正在骑乘生物信息"""
        return self.comp_factory.CreateRide(self.id).GetEntityRider()

    # 增加坐骑座位
    def AddRideSeat(self, pos, rot, lock_rot):
        # type: (tuple, float, float) -> int
        """
        增加坐骑座位\n
        - 返回添加最大索引
        """
        return self.comp_factory.CreateRide(self.id).AddEntitySeat(pos, rot, lock_rot)

    # 删除坐骑座位
    def DelRideSeat(self, index):
        # type: (int) -> bool
        """
        删除坐骑座位\n
        - 只能删除通过接口AddEntitySeat添加的座位
        """
        return self.comp_factory.CreateRide(self.id).DeleteEntitySeat(index)

    # 设置坐骑座位的位置、旋转以及允许实体旋转范围
    def SetRideSeat(self, index, pos, rot, lock_rot):
        # type: (int, tuple, float, float) -> bool
        """设置坐骑座位的位置、旋转以及允许实体旋转范围"""
        return self.comp_factory.CreateRide(self.id).SetEntitySeat(index, pos, rot, lock_rot)

    # 设置坐骑上的实体是否锁定序号
    def SetRideSeatLock(self, lock):
        # type: (bool) -> bool
        """
        设置坐骑上的实体是否锁定序号\n
        - 锁定以后，该坐骑上某个实体停止骑乘后不会改变坐骑上其他实体当前所处的座位。
        - 解除锁定后，不会立刻改变坐骑上骑乘者的位置。
        """
        return self.comp_factory.CreateRide(self.id).SetEntityLockRider(lock)

    # 设置骑乘者在当前坐骑上的序号
    def ChangeRideSeat(self, index):
        # type: (int) -> bool
        """设置骑乘者在当前坐骑上的序号"""
        return self.comp_factory.CreateRide(self.id).ChangeRiderSeat(index)

    # 设置实体骑乘生物
    def SetRideEntity(self, ride_id, index=None):
        # type: (str, int) -> bool
        """设置实体骑乘生物（或者船与矿车）"""
        ride_comp = self.comp_factory.CreateRide(serverApi.GetLevelId())
        return ride_comp.SetRiderRideEntity(self.id, ride_id, index)

    # -----------------------------------------------------------------------------------

    def GetSelectedItem(self):
        # type: () -> dict
        """获得手持物字典"""
        item_comp = self.comp_factory.CreateItem(self.id)
        item_dict = item_comp.GetEntityItem(ItemPosType.CARRIED, getUserData=True)
        if not item_dict:
            item_dict = {}
        return item_dict

    def SetSelectedItem(self, itemDict):
        # type: (dict) -> bool
        """设置生物主手物品"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetEntityItem(ItemPosType.CARRIED, itemDict)

    def GetSelectedItemName(self):
        # type: () -> str
        """获得主手物品Id"""
        return self.GetSelectedItem().get("newItemName", "")

    def GetOffhandItem(self):
        # type: () -> dict
        """获得副手物品字典"""
        item_comp = self.comp_factory.CreateItem(self.id)
        itemDict = item_comp.GetEntityItem(ItemPosType.OFFHAND, getUserData=True)
        if not itemDict:
            itemDict = {}
        return itemDict

    def GetOffhandItemName(self):
        # type: () -> str
        """获得副手物品Id"""
        return self.GetOffhandItem().get("newItemName", "")

    def SetOffhandItem(self, itemDict):
        # type: (dict) -> bool
        """设置生物副手物品"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetEntityItem(ItemPosType.OFFHAND, itemDict)

    def GetArmorItem(self, slotPos):
        # type: (int) -> dict
        """获取实体的装备"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.GetEntityItem(ItemPosType.ARMOR, slotPos, True)

    def GetArmorItemName(self, slot_pos):
        # type: (int) -> str
        """获得实体装备名称"""
        item = self.GetArmorItem(slot_pos)
        if not item:
            return ""
        return item["newItemName"]

    def GetHelmetItem(self):
        # type: () -> dict
        """获得实体头盔物品"""
        return self.GetArmorItem(0)

    def GetChestplateItem(self):
        # type: () -> dict
        """获得实体胸甲物品"""
        return self.GetArmorItem(1)

    def GetLeggingsItem(self):
        # type: () -> dict
        """获得实体护腿物品"""
        return self.GetArmorItem(2)

    def GetBootsItem(self):
        # type: () -> dict
        """获得实体护腿物品"""
        return self.GetArmorItem(3)

    def SetArmorItem(self, slotPos, itemDict):
        # type: (int, dict) -> bool
        """设置实体的装备"""
        item_comp = self.comp_factory.CreateItem(self.id)
        return item_comp.SetEntityItem(ItemPosType.ARMOR, itemDict, slotPos)
